home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / ZFILTER.C < prev    next >
C/C++ Source or Header  |  1992-02-15  |  5KB  |  182 lines

  1. /* Copyright (C) 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zfilter.c */
  21. /* Filter creation for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "alloc.h"
  26. #include "stream.h"
  27.  
  28. /* Forward references */
  29. int filter_read(P3(os_ptr, stream_procs _ds *, stream **));
  30. int filter_write(P3(os_ptr, stream_procs _ds *, stream **));
  31.  
  32. /* .filterASCIIHexEncode */
  33. extern stream_procs s_AXE_procs;
  34. int
  35. zAXE(os_ptr op)
  36. {    return filter_write(op, &s_AXE_procs, NULL);
  37. }
  38.  
  39. /* .filterASCIIHexDecode */
  40. extern stream_procs s_AXD_procs;
  41. extern void s_AXD_init(P1(stream *));
  42. int
  43. zAXD(os_ptr op)
  44. {    stream *s;
  45.     int code = filter_read(op, &s_AXD_procs, &s);
  46.     if ( code < 0 ) return code;
  47.     s_AXD_init(s);
  48.     return code;
  49. }
  50.  
  51. /* .filtereexecDecode */
  52. extern stream_procs s_exD_procs;
  53. extern void s_exD_init(P2(stream *, ushort));
  54. int
  55. zexD(register os_ptr op)
  56. {    stream *s;
  57.     ushort state;
  58.     int code;
  59.     check_type(*op, t_integer);
  60.     state = op->value.intval;
  61.     if ( op->value.intval != state )
  62.         return e_rangecheck;    /* state value was truncated */
  63.     code = filter_read(op - 1, &s_exD_procs, &s);
  64.     if ( code < 0 ) return code;
  65.     s_exD_init(s, state);
  66.     pop(1);
  67.     return 0;
  68. }
  69.  
  70. /* .filterPFBDecode */
  71. extern stream_procs s_PFBD_procs;
  72. extern void s_PFBD_init(P1(stream *));
  73. int
  74. zPFBD(os_ptr op)
  75. {    stream *s;
  76.     int code = filter_read(op, &s_PFBD_procs, &s);
  77.     if ( code < 0 ) return code;
  78.     s_PFBD_init(s);
  79.     return code;
  80. }
  81.  
  82. /* ------ Utilities ------ */
  83.  
  84. /* Free the underlying string stream if needed. */
  85. private void
  86. filter_free_null(stream *s)
  87. {
  88. }
  89. private void
  90. filter_free_stream(stream *s)
  91. {    alloc_free((char *)s->strm, 1, sizeof(stream),
  92.            "filter_free_stream(stream)");
  93. }
  94.  
  95. /* Set up an input filter. */
  96. int
  97. filter_read(os_ptr op, stream_procs _ds *procs, stream **ps)
  98. {    stream *s;
  99.     stream *sstrm;
  100.     int is_temp;
  101.     int code;
  102.     /* Check to make sure that the underlying data */
  103.     /* can function as a source for reading. */
  104.     switch ( r_type(op) )
  105.        {
  106.     case t_string:
  107.         check_access(*op, a_read);
  108.         sstrm = (stream *)alloc(1, sizeof(stream),
  109.                     "filter_read(string stream)");
  110.         if ( sstrm == 0 ) return e_VMerror;
  111.         sread_string(sstrm, op->value.bytes, r_size(op));
  112.         is_temp = 1;
  113.         break;
  114.     case t_file:
  115.         check_access(*op, a_read);
  116.         sstrm = op->value.pfile;
  117.         is_temp = 0;
  118.         break;
  119.     default:
  120.         return e_typecheck;
  121.        }
  122.     code = file_open((byte *)0, 0, "r", (ref *)op, &s);
  123.     if ( code < 0 ) return code;
  124.     s_std_init(s, s->cbuf, s->bsize, procs, s_mode_read);
  125.     s->end_status = 0;
  126.     s->position = -1;        /* not positionable */
  127.     s->file = 0;            /* not a file stream */
  128.     s->strm = sstrm;
  129.     s->strm_is_temp = is_temp;
  130.     if ( ps != NULL ) *ps = s;
  131.     return 0;
  132. }
  133.  
  134. /* Set up an output filter. */
  135. int
  136. filter_write(os_ptr op, stream_procs _ds *procs, stream **ps)
  137. {    stream *s;
  138.     stream *sstrm;
  139.     int is_temp;
  140.     int code;
  141.     /* Check to make sure that the underlying data */
  142.     /* can function as a sink for writing. */
  143.     switch ( r_type(op) )
  144.        {
  145.     case t_string:
  146.         check_access(*op, a_write);
  147.         sstrm = (stream *)alloc(1, sizeof(stream),
  148.                     "filter_write(string)");
  149.         if ( sstrm == 0 ) return e_VMerror;
  150.         swrite_string(sstrm, op->value.bytes, r_size(op));
  151.         is_temp = 1;
  152.         break;
  153.     case t_file:
  154.         check_access(*op, a_write);
  155.         sstrm = op->value.pfile;
  156.         is_temp = 0;
  157.         break;
  158.     default:
  159.         return e_typecheck;
  160.        }
  161.     code = file_open((byte *)0, 0, "w", (ref *)op, &s);
  162.     if ( code < 0 ) return code;
  163.     s_std_init(s, s->cbuf, s->bsize, procs, s_mode_write);
  164.     s->end_status = 0;
  165.     s->position = -1;        /* not positionable */
  166.     s->file = 0;            /* not a file stream */
  167.     s->strm = sstrm;
  168.     s->strm_is_temp = is_temp;
  169.     if ( ps != NULL ) *ps = s;
  170.     return 0;
  171. }
  172.  
  173. /* ------ Initialization procedure ------ */
  174.  
  175. op_def zfilter_op_defs[] = {
  176.     {"1.filterASCIIHexEncode", zAXE},
  177.     {"1.filterASCIIHexDecode", zAXD},
  178.     {"2.filtereexecDecode", zexD},
  179.     {"1.filterPFBDecode", zPFBD},
  180.     op_def_end(0)
  181. };
  182.